package io.renren.util;

import org.springframework.stereotype.Component;

import java.awt.*;
import java.awt.print.*;

@Component
public class PrintUtils {

    public void print(){
        int height = 175 + 3 * 15 + 20;

        // 通俗理解就是书、文档
        Book book = new Book();

        // 打印格式
        PageFormat pf = new PageFormat();
        pf.setOrientation(PageFormat.PORTRAIT);

        // 通过Paper设置页面的空白边距和可打印区域。必须与实际打印纸张大小相符。
        Paper p = new Paper();
        p.setSize(230, height);
        p.setImageableArea(5, -20, 230, height + 20);
        pf.setPaper(p);

        // 把 PageFormat 和 Printable 添加到书中，组成一个页面
        book.append(new Prient(), pf);

        System.out.println("****************************************************************");
        System.out.println("kai shi da yin le");
        System.out.println("****************************************************************");


        // 获取打印服务对象
        /*
        PrinterJob job = PrinterJob.getPrinterJob();
        job.setPageable(book);
        try {
            job.print();
        } catch (PrinterException e) {
            System.out.println("================打印出现异常");
        }
        */

    }

    class Prient implements Printable {

        @Override
        public int print(Graphics g, PageFormat pf, int page) throws PrinterException {

            if (page > 0) {
                return NO_SUCH_PAGE;
            }

            Graphics2D g2d = (Graphics2D) g;
            g2d.setFont(new Font("Default", Font.PLAIN, 20));
            g2d.drawString("消费清单", 50, 10);
            g2d.drawString("-------------------------------------", 7, 145);
            g2d.setFont(new Font("Default", Font.PLAIN, 12));
            g2d.drawString("店名：" + "11", 7, 175);
            g2d.drawString("客户手机号码：" + "13112345678", 7, 35);
            g2d.drawString("打印时间:" + "1111" + "", 7, 160);
            g2d.drawString("操作人:" + "小敏" + "", 7, 160);
            g2d.drawString("-------------------------------------", 7, 20);
            g2d.setFont(new Font("Default", Font.PLAIN, 12));
            g2d.drawString("产品名称        数量      单价      金额", 5, 10);
            g2d.drawString("-------------------------------------", 7, 20);
            g2d.drawString("全身推拿        1      50(会员价:40)      40", 5, 10);
            g2d.drawString("局部推拿        1      30(会员价:25)      25", 5, 10);
            g2d.drawString("烤电        1      15      15", 5, 10);
            g2d.drawString("-------------------------------------", 7, 20);
            g2d.drawString("应付金额                80", 7, 130);


            return PAGE_EXISTS;
        }
    }
}
